package com.labor_management_system.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.labor_management_system.Entity.Attendance;
import com.labor_management_system.Mapper.AttendanceMapper;
import com.labor_management_system.Service.AttendanceService;
import com.labor_management_system.Utils.Pair;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;

import java.util.ArrayList;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.labor_management_system.Utils.TimeUtil.*;


@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
    implements AttendanceService {
    @Override
    public List<Pair<Integer,Double>> getEmployeeAttendance(String employeeId, Long currentTime) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("employee_id",employeeId);
        wrapper.between("start_work",getStartOfMonthTimestamp(currentTime),getEndOfMonthTimestamp(currentTime));

        List<Attendance> temp = baseMapper.selectList(wrapper);

        // 首先，根据startWork对temp列表进行排序
        List<Attendance> sortedTemp = temp.stream()
                .sorted(Comparator.comparingLong(Attendance::getStartWork))
                .collect(Collectors.toList());

        List<Pair<Integer,Double>> times = new ArrayList<>(31);
        for(Attendance attendance : sortedTemp){
            Duration duration = Duration.between(Instant.ofEpochMilli(attendance.getStartWork()), Instant.ofEpochMilli(attendance.getEndWork()));
            times.add(new Pair<>(getDayOfMonth(attendance.getStartWork()),duration.toHours() + duration.toMinutes() / 60.0));
        }
        return times;

    }
    @Override
    public Long getWorkingNumber(List<String> employeeIDs, Long currentTime) {
        if(employeeIDs==null)return null;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.between("start_work",getMidnightTimestampOfToday(currentTime),getMidnightTimestampOfTomorrow(currentTime));
        List<Attendance> attendances = baseMapper.selectList(wrapper);
        List<Attendance> filteredAttendances = new ArrayList<>();
        Long L11 =0L;
        System.out.println(getMidnightTimestampOfToday(currentTime)+"\n");
        System.out.println(getMidnightTimestampOfTomorrow(currentTime)+"\n");

        //TODO:缺少判断条件
        for (Attendance attendance : attendances){
            if (employeeIDs.contains(attendance.getEmployeeId())){
                if(filteredAttendances.contains(attendance)){
                    continue;
                }
                System.out.println(attendance.getEmployeeId()+"\n");
                filteredAttendances.add(attendance);
                L11++;
            }
        }
        return L11;
    }

    @Override
    public Pair<Long, Double> getYWorkNumber(List<String> employeeIDs, Long currentTime) {
        if (employeeIDs == null) return null;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.between("start_work", getMidnightTimestampOfYesterday(currentTime), getMidnightTimestampOfToday(currentTime));
        List<Attendance> attendances = baseMapper.selectList(wrapper);
        List<Attendance> filteredAttendances = new ArrayList<>();
        Long L11 =0L;
        double cultime = 0;
        for (Attendance attendance : attendances) {
            if (employeeIDs.contains(attendance.getEmployeeId())) {
                Duration duration = Duration.between(Instant.ofEpochMilli(attendance.getStartWork()), Instant.ofEpochMilli(attendance.getEndWork()));
                cultime += duration.toHours() + duration.toMinutes() / 60.0;
                if(filteredAttendances.contains(attendance)){
                    continue;
                }
                L11+=1;
                filteredAttendances.add(attendance);
            }
        }
        return new Pair<>(L11, cultime);
    }


    @Override
    public Pair<Long, Double> getMWorkNumber(List<String> employeeIDs, Long currentTime) {
        if (employeeIDs == null) return null;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.between("start_work", getStartOfMonthTimestamp(currentTime), getEndOfMonthTimestamp(currentTime));
        List<Attendance> attendances = baseMapper.selectList(wrapper);
        List<Attendance> filteredAttendances = new ArrayList<>();
        Long L11 =0L;
        double cultime = 0;
        for (Attendance attendance : attendances) {
            if (employeeIDs.contains(attendance.getEmployeeId())) {
                Duration duration = Duration.between(Instant.ofEpochMilli(attendance.getStartWork()), Instant.ofEpochMilli(attendance.getEndWork()));
                cultime += duration.toHours() + duration.toMinutes() / 60.0;
                if(filteredAttendances.contains(attendance)){
                    continue;
                }
                L11+=1;
                filteredAttendances.add(attendance);
            }
        }
        return new Pair<>(L11, cultime);
    }




    //考勤

}




